package com.kangye.exam.controller;


import com.kangye.exam.bean.Paper;
import com.kangye.exam.bean.Student;
import com.kangye.exam.bean.TeacherPaper;
import com.kangye.exam.service.StudentExamRecordService;
import com.kangye.exam.utils.JsonUtil;
import com.kangye.exam.utils.RedisTool;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/record")
public class StudentExamRecord {
    @Resource
    private RedisTool redisTool;
    @Resource
    private StudentExamRecordService strs;

    //考试记录页面加载
    @RequestMapping("/loading")
    public Object test_center_lording(HttpServletRequest req, HttpServletResponse resp, Integer stuId) {
        String session_id = req.getSession().getId();
        Map<String,Object> map = new HashMap<>();
        Student student = new Student();
        if (stuId==null){
            map.put("code",-1);//学生id不能为null
            return map;
        }
        if ( redisTool.hget("stu",session_id)!=null){
            String stu = redisTool.hget("stu",session_id).toString();//获取redis中学生对象
            student = JsonUtil.json2Bean(stu,Student.class);
        }else{
            student.setStudentId(stuId);
        }
        List<TeacherPaper> papers = strs.testLoading(student);     //获取试卷
        if (papers.size()==0){
            map.put("code",-1);//没有可以查看的记录
            return map;
        }
        redisTool.hset("test_round_list",session_id,JsonUtil.list2Json(papers),3600);//把获取的试卷列表存入redis中
        map.put("papers",papers);
        map.put("code",0);
        return map;
    }
    //加载试卷
    @RequestMapping("/paper_loading")
    public Object paper_lording(HttpSession session,Integer stuId,Integer paperId){
        Map<String,Object> map = new HashMap<>();
        String session_id = session.getId();
        if (paperId==null){
            map.put("code",-1);//传参不合法
            return map;
        }
        TeacherPaper paper = new TeacherPaper();
        if (redisTool.hget("test_round_list",session_id)!=null){
            String paperlist = redisTool.hget("test_round_list",session_id).toString();//根据session的id取出试卷表
            List<TeacherPaper> papers = JsonUtil.json2List(paperlist, Paper.class);
            for (TeacherPaper p: papers) {
                if (p.getPaperId()==paperId){
                    paper = p;
                }
            }
        }else{
            paper.setPaperId(paperId);
            paper.setStudentId(stuId);
            paper = strs.getPaper(paper);
        }

        if (paper.getCorrectingPath()==null){
            map.put("code",-1);//传参不合法
            return map;
        }
        String paperUrl = paper.getCorrectingPath();//获取到试卷路径
        //根据路径获取到文件
        //下列整合时需改
        String path = Thread.currentThread().getContextClassLoader().getResource("").getPath()+ "paper/"+paperUrl+".json";
        File file = new File(path);
        if (!file.exists()){
            map.put("code",-1);    //如果文件不存在则
            return map;
        }
        String str = this.jsonRead(file);//解析json文件为String格式
        map.put("code",0);
        map.put("paper",str);
        return map;
    }
    //文件的读取
    private String jsonRead(File file){
        String jsonStr;
        try {
            FileReader fileReader = new FileReader(file);//
            Reader reader = new InputStreamReader(new FileInputStream(file),"utf-8");
            int ch=0;
            StringBuffer sb = new StringBuffer();
            while ((ch=reader.read())!=-1){
                sb.append((char)ch);
            }
            fileReader.close();
            jsonStr = sb.toString();
            return jsonStr;
        } catch (Exception e) {
            return null;
        }
    }
}
